<<<<<<< HEAD
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Statistics extends CI_Controller {
	
	private $tab ='datastatistics';
	
	function __construct(){
		parent::__construct();
		/** 登陆验证 */
		if(!isset($_SESSION)){
		    session_start();
		}
		$admin = $this->session->userdata('admin_user');
		if(!$admin || !isset($admin['user_id'])){
			header('Location: /sys_admin/login');
			exit;
		}
	}
	
	
	/*
	 * 充值统计表
	 */
	function recharge(){
		//****** by liub 20141111 
		$data = array();
		$per_page 	= $this->input->get('per_page')?$this->input->get('per_page'):0;
		$strtime	= $this->input->get('strtime');
		$endtime	= $this->input->get('endtime');
			if($strtime != ''){
			$data['where']['strtime'] = $strtime;
		}
		if($endtime != ''){
			$data['where']['endtime'] = $endtime;
		}
		$data['page_size']  = STATISTICS_LIST_PAGE_SIZE;//每页行数
		$data['page_start'] = (int)$per_page;//起始页
		$this->load->model('Accountrecharge_model');
		$vdata = $this->Accountrecharge_model->get_recharge_statistics($data);
		
		//分页
		$this->load->library('pagination');
		$config['base_url'] = '/sys_admin/statistics/recharge/?strtime='.$strtime.'&endtime='.$endtime;
		$config['total_rows'] = $vdata['statisticsrecharge_count'];
		$config['per_page'] = STATISTICS_LIST_PAGE_SIZE; 
		$this->pagination->initialize($config); 
		
		$vdata['tab'] = $this->tab;
		$vdata['sub'] = 'rechargecount';
		$this->load->view('/sys_admin/statistics/recharge', $vdata);
	}
	
	
	/*
	 * 提现统计表
	 */
	function withdraw(){
		//****** by liub 20141111 
		$data = array();
		$per_page 	= $this->input->get('per_page')?$this->input->get('per_page'):0;
		$strtime	= $this->input->get('strtime');
		$endtime	= $this->input->get('endtime');
			if($strtime != ''){
			$data['where']['strtime'] = $strtime;
		}
		if($endtime != ''){
			$data['where']['endtime'] = $endtime;
		}
		$data['page_size']  = STATISTICS_LIST_PAGE_SIZE;//每页行数
		$data['page_start'] = (int)$per_page;//起始页
		$this->load->model('Accountcash_model');
		$vdata = $this->Accountcash_model->get_accountcash_statistics($data);
		
		//分页
		$this->load->library('pagination');
		$config['base_url'] = '/sys_admin/statistics/withdraw/?strtime='.$strtime.'&endtime='.$endtime;
		$config['total_rows'] = $vdata['accountcash_count'];
		$config['per_page'] = STATISTICS_LIST_PAGE_SIZE; 
		$this->pagination->initialize($config); 
		
		$vdata['tab'] = $this->tab;
		$vdata['sub'] = 'withdrawcount';
		
		$this->load->view('/sys_admin/statistics/withdraw', $vdata);
	}
	
	
	/*
	 * 平台收入统计表
	 */
	function platform(){
		
		//****** by liub 20141111 
		$data = array();
		$per_page 	= $this->input->get('per_page')?$this->input->get('per_page'):0;
		$strtime	= $this->input->get('strtime');
		$endtime	= $this->input->get('endtime');
		
		if($strtime != ''){
			$data['where']['strtime'] = $strtime;
		}
		if($endtime != ''){
			$data['where']['endtime'] = $endtime;
		}
		$data['page_size']  = STATISTICS_LIST_PAGE_SIZE;//每页行数
		$data['page_start'] = (int)$per_page;//起始页
		
		
		$this->load->model('Accountrecharge_model');
		$vdata = $this->Accountrecharge_model->get_platform_statistics($data);

		//分页
		$this->load->library('pagination');
		$config['base_url'] = '/sys_admin/statistics/platform/?strtime='.$strtime.'&endtime='.$endtime;
		$config['total_rows'] = $vdata['statisticsplatform_count'];;
		$config['per_page'] = STATISTICS_LIST_PAGE_SIZE; 
		$this->pagination->initialize($config); 
		$vdata['tab'] = $this->tab;
		$vdata['sub'] = 'platformcount';
		
		$this->load->view('/sys_admin/statistics/platform', $vdata);
	}
	
	
	/*
	 * 平台浮存统计表
	 */
	function floatcount(){
	$vdata['tab'] = $this->tab;
		$vdata['sub'] = 'floatcount';
		
		$this->load->model('Accountrecharge_model');
		$vdata = $this->Accountrecharge_model->queryFloatCount();
		
		$this->load->view('/sys_admin/statistics/floatcount', $vdata);//TODO  暂略
		
		//TODO  暂略
		/**
		$data = array();
		$per_page 	= $this->input->get('per_page')?$this->input->get('per_page'):0;
		$strtime	= $this->input->get('strtime');
		$endtime	= $this->input->get('endtime');
		
		if($strtime != ''){
			$data['where']['strtime'] = $strtime;
		}
		if($endtime != ''){
			$data['where']['endtime'] = $endtime;
		}
		$data['page_size']  = STATISTICS_LIST_PAGE_SIZE;//每页行数
		$data['page_start'] = (int)$per_page;//起始页
		
		
		$this->load->model('User_model');
		$vdata = $this->User_model->statistics_user($data);
		
		$this->load->view('/sys_admin/statistics/floatcount', $vdata);
		*/
		
		//$vdata['tab'] = $this->tab;
		//$vdata['sub'] = 'platformcount';
	}
	
	
	/*
	 * 风险金准备金统计分析表
	 */
	function riskcount(){
	
			
		//TODO  暂略
	$vdata['tab'] = $this->tab;
		$vdata['sub'] = 'riskcount';
		
		$this->load->view('/sys_admin/statistics/riskcount', $vdata);
	}
	
	
	/*
	 * 会员数据统计分析表
	 */
	function udatacount(){
		
		$data = array();
		$per_page 	= $this->input->get('per_page')?$this->input->get('per_page'):0;
		$strtime	= $this->input->get('strtime');
		$endtime	= $this->input->get('endtime');
		
		if($strtime != ''){
			$data['where']['strtime'] = $strtime;
		}
		if($endtime != ''){
			$data['where']['endtime'] = $endtime;
		}
		$data['page_size']  = STATISTICS_LIST_PAGE_SIZE;//每页行数
		$data['page_start'] = (int)$per_page;//起始页
		
		$this->load->model('User_model');
		$vdata = $this->User_model->statistics_user($data);

		//分页
		$this->load->library('pagination');
		$config['base_url'] = '/sys_admin/statistics/udatacount/?strtime='.$strtime.'&endtime='.$endtime;
		$config['total_rows'] = $vdata['time_count'];;
		$config['per_page'] = STATISTICS_LIST_PAGE_SIZE; 
		$this->pagination->initialize($config); 
		$vdata['tab'] = $this->tab;
		$vdata['sub'] = 'usercount';
		
		$this->load->view('/sys_admin/statistics/udatacount', $vdata);
	}
	
	
	/*
	 * 理财情况统计分析表
	 */
	function managemoney($type = "default"){
		$year 	= $_REQUEST['year'];
		$month 	= $_REQUEST['month'];
		$ji		= $_REQUEST['ji'];
		
		$tstart = trim($_REQUEST['tstart']);
		$tend 	= trim($_REQUEST['tend']);
		
		$vdata['tab'] = $this->tab;
		$vdata['sub'] = 'managecount';

		$currt = strtotime("2015-01-01");
		$start = strtotime("2015-01-01");
		
		//$currt = strtotime("2014-02-01");
		//$start = strtotime("2014-02-01");
		
		if($tstart != ""){
			$currt = strtotime($tstart.'-01');
			$start = strtotime($tstart.'-01');
		}
		$end   = strtotime(date("Y-m-01"));
		$end 	= strtotime("1 month",$end);
		if($tend != ""){
			$end = strtotime($tend.'-01');
			$end = strtotime("1 month",$end);
		}
		$this->load->model('Borrow_model');
		$nstart = 0;
		$index = 0;
		do{
			$nstart = strtotime("1 month",$start);
			$vdata['list'][$index] = $this->Borrow_model->managemoney(array('start_time' =>$start, 'end_time'=> $nstart));
			$vdata['list'][$index]['year'] = date("Y",$start);
			$vdata['list'][$index]['month'] = date("m",$start);
			$start = strtotime("1 month",$start);
			$index++;
		}while($start != $end);
		
		$vdata['tstart'] 	= $tstart;
		$vdata['tend'] 	= $tend;
		
		if($type == 'caccount'){
			self::array_sort($vdata['list'],'caccount','DESC');
		}elseif($type == 'saccount'){
			self::array_sort($vdata['list'],'saccount','DESC');
		}elseif($type == 'useracount'){
			self::array_sort($vdata['list'],'useracount','DESC');
		}elseif($type == 'museracount'){
			self::array_sort($vdata['list'],'museracount','DESC');
		}elseif($type == 'rjtmoney'){
			self::array_sort($vdata['list'],'rjtmoney','DESC');
		}elseif($type == 'baccount'){
			self::array_sort($vdata['list'],'baccount','DESC');
		}elseif($type == 'rjcnum'){
			self::array_sort($vdata['list'],'rjcnum','DESC');
		}elseif($type == 'rjye'){
			self::array_sort($vdata['list'],'rjye','DESC');
		}
		//TODO  暂略
		$this->load->view('/sys_admin/statistics/managemoney', $vdata);
	}
	
	
	function managemoneyexcel($type = "default"){
		$year 	= $_REQUEST['year'];
		$month 	= $_REQUEST['month'];
		$ji		= $_REQUEST['ji'];
		
		$tstart = trim($_REQUEST['tstart']);
		$tend 	= trim($_REQUEST['tend']);
		
		$vdata['tab'] = $this->tab;
		$vdata['sub'] = 'managecount';
		$currt = strtotime("2014-02-01");
		$start = strtotime("2014-02-01");
		if($tstart != ""){
			$currt = strtotime($tstart.'-01');
			$start = strtotime($tstart.'-01');
		}
		$end   = strtotime(date("Y-m-01"));
		$end 	= strtotime("1 month",$end);
		if($tend != ""){
			$end = strtotime($tend.'-01');
			$end = strtotime("1 month",$end);
		}
		$this->load->model('Borrow_model');
		$nstart = 0;
		$index = 0;
		do{
			$nstart = strtotime("1 month",$start);
			$vdata['list'][$index] = $this->Borrow_model->managemoney(array('start_time' =>$start, 'end_time'=> $nstart));
			$vdata['list'][$index]['year'] = date("Y",$start);
			$vdata['list'][$index]['month'] = date("m",$start);
			$start = strtotime("1 month",$start);
			$index++;
		}while($start != $end);
		
		if($type == 'caccount'){
			self::array_sort($vdata['list'],'caccount','DESC');
		}elseif($type == 'saccount'){
			self::array_sort($vdata['list'],'saccount','DESC');
		}elseif($type == 'useracount'){
			self::array_sort($vdata['list'],'useracount','DESC');
		}elseif($type == 'museracount'){
			self::array_sort($vdata['list'],'museracount','DESC');
		}elseif($type == 'rjtmoney'){
			self::array_sort($vdata['list'],'rjtmoney','DESC');
		}elseif($type == 'baccount'){
			self::array_sort($vdata['list'],'baccount','DESC');
		}elseif($type == 'rjcnum'){
			self::array_sort($vdata['list'],'rjcnum','DESC');
		}elseif($type == 'rjye'){
			self::array_sort($vdata['list'],'rjye','DESC');
		}
		
		$this->load->library('PHPExcel');
		$this->load->library('PHPExcel/IOFactory'); 
		$objPHPExcel = new PHPExcel();
		$objPHPExcel->getProperties()->setTitle("理财情况统计分析表")->setDescription(COMPANY_NAME); 
		
		$objPHPExcel->setActiveSheetIndex(0);
		$titles = array("序号", "年", "月", "累计理财投标总额", "本月新增理财总额", "累计理财会员数", "新增理财会员数", "人均理财金额", "每标人均投标金额", "人均投资债权标数量", "人均账户余额");
		
		for($i = 0; $i < count($titles); $i ++){
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($i, 1, $titles[$i]);
		}
		$account_list = $vdata['list'];
		for($i = 0; $i < count($account_list); $i++){
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(0, $i+2, $i+1);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(1, $i+2, $account_list[$i]['year']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(2, $i+2, $account_list[$i]['month']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(3, $i+2, $account_list[$i]['caccount']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(4, $i+2, $account_list[$i]['saccount']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(5, $i+2, $account_list[$i]['useracount']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(6, $i+2, $account_list[$i]['museracount']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(7, $i+2, $account_list[$i]['rjtmoney']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(8, $i+2, $account_list[$i]['baccount']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(9, $i+2, $account_list[$i]['rjcnum']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(10, $i+2, round($account_list[$i]['rjye'],2));
		}
		$objWriter = IOFactory::createWriter($objPHPExcel, 'Excel5'); 
		//发送标题强制用户下载文件 
		header('Content-Type: application/vnd.ms-excel'); 
		header('Content-Disposition: attachment;filename="理财情况统计分析表.xls"'); 
		header('Cache-Control: max-age=0'); 
		$objWriter->save('php://output'); 
	}
	
	function array_sort($array,$keys,$type='asc'){
	    if(!isset($array) || !is_array($array) || empty($array)){
	        return '';
	    }
	    if(!isset($keys) || trim($keys)==''){
	        return '';
	    }
	    if(!isset($type) || $type=='' || !in_array(strtolower($type),array('asc','desc'))){
	        return '';
	    }
	    $keysvalue=array();
	    foreach($array as $key=>$val){
	        $val[$keys] = str_replace('-','',$val[$keys]);
	        $val[$keys] = str_replace(' ','',$val[$keys]);
	        $val[$keys] = str_replace(':','',$val[$keys]);
	        $keysvalue[] =$val[$keys];
	    }
	    asort($keysvalue); //key值排序
	    reset($keysvalue); //指针重新指向数组第一个
	    foreach($keysvalue as $key=>$vals) {
	        $keysort[] = $key;
	    }
	    $keysvalue = array();
	    $count=count($keysort);
	    if(strtolower($type) != 'asc'){
	        for($i=$count-1; $i>=0; $i--) {
	            $keysvalue[] = $array[$keysort[$i]];
	        }
	    }else{
	        for($i=0; $i<$count; $i++){
	            $keysvalue[] = $array[$keysort[$i]];
	        }
	    }
	    return $keysvalue;
	}
	
	/*
	 * 流量统计分析
	 */
	function flowcount(){
		$vdata['tab'] = $this->tab;
		$vdata['sub'] = 'flowcount';
		
			
		//TODO  暂略
		
		$this->load->view('/sys_admin/statistics/flowcount', $vdata);
	}
	
	
	
=======
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Statistics extends CI_Controller {
    
	private $tab ='datastatistics';
	
	function __construct(){
		parent::__construct();
		/** 登陆验证 */
		if(!isset($_SESSION)){
		    session_start();
		}
		$admin = $this->session->userdata('admin_user');
		if(!$admin || !isset($admin['user_id'])){
			header('Location: /sys_admin/login');
			exit;
		}
	}
	
	
	/*
	 * 充值统计表
	 */
	function recharge(){
		//****** by liub 20141111 
		$data = array();
		$per_page 	= $this->input->get('per_page')?$this->input->get('per_page'):0;
		$strtime	= $this->input->get('strtime');
		$endtime	= $this->input->get('endtime');
			if($strtime != ''){
			$data['where']['strtime'] = $strtime;
		}
		if($endtime != ''){
			$data['where']['endtime'] = $endtime;
		}
		$data['page_size']  = STATISTICS_LIST_PAGE_SIZE;//每页行数
		$data['page_start'] = (int)$per_page;//起始页
		$this->load->model('Accountrecharge_model');
		$vdata = $this->Accountrecharge_model->get_recharge_statistics($data);
		
		//分页
		$this->load->library('pagination');
		$config['base_url'] = '/sys_admin/statistics/recharge/?strtime='.$strtime.'&endtime='.$endtime;
		$config['total_rows'] = $vdata['statisticsrecharge_count'];
		$config['per_page'] = STATISTICS_LIST_PAGE_SIZE; 
		$this->pagination->initialize($config); 
		
		$vdata['tab'] = $this->tab;
		$vdata['sub'] = 'rechargecount';
		$this->load->view('/sys_admin/statistics/recharge', $vdata);
	}
	
	
	/*
	 * 提现统计表
	 */
	function withdraw(){
		//****** by liub 20141111 
		$data = array();
		$per_page 	= $this->input->get('per_page')?$this->input->get('per_page'):0;
		$strtime	= $this->input->get('strtime');
		$endtime	= $this->input->get('endtime');
			if($strtime != ''){
			$data['where']['strtime'] = $strtime;
		}
		if($endtime != ''){
			$data['where']['endtime'] = $endtime;
		}
		$data['page_size']  = STATISTICS_LIST_PAGE_SIZE;//每页行数
		$data['page_start'] = (int)$per_page;//起始页
		$this->load->model('Accountcash_model');
		$vdata = $this->Accountcash_model->get_accountcash_statistics($data);
		
		//分页
		$this->load->library('pagination');
		$config['base_url'] = '/sys_admin/statistics/withdraw/?strtime='.$strtime.'&endtime='.$endtime;
		$config['total_rows'] = $vdata['accountcash_count'];
		$config['per_page'] = STATISTICS_LIST_PAGE_SIZE; 
		$this->pagination->initialize($config); 
		
		$vdata['tab'] = $this->tab;
		$vdata['sub'] = 'withdrawcount';
		
		$this->load->view('/sys_admin/statistics/withdraw', $vdata);
	}
	
	
	/*
	 * 平台收入统计表
	 */
	function platform(){
		
		//****** by liub 20141111 
		$data = array();
		$per_page 	= $this->input->get('per_page')?$this->input->get('per_page'):0;
		$strtime	= $this->input->get('strtime');
		$endtime	= $this->input->get('endtime');
		
		if($strtime != ''){
			$data['where']['strtime'] = $strtime;
		}
		if($endtime != ''){
			$data['where']['endtime'] = $endtime;
		}
		$data['page_size']  = STATISTICS_LIST_PAGE_SIZE;//每页行数
		$data['page_start'] = (int)$per_page;//起始页
		
		
		$this->load->model('Accountrecharge_model');
		$vdata = $this->Accountrecharge_model->get_platform_statistics($data);

		//分页
		$this->load->library('pagination');
		$config['base_url'] = '/sys_admin/statistics/platform/?strtime='.$strtime.'&endtime='.$endtime;
		$config['total_rows'] = $vdata['statisticsplatform_count'];;
		$config['per_page'] = STATISTICS_LIST_PAGE_SIZE; 
		$this->pagination->initialize($config); 
		$vdata['tab'] = $this->tab;
		$vdata['sub'] = 'platformcount';
		
		$this->load->view('/sys_admin/statistics/platform', $vdata);
	}
	
	
	/*
	 * 平台浮存统计表
	 */
	function floatcount(){
	$vdata['tab'] = $this->tab;
		$vdata['sub'] = 'floatcount';
		
		$this->load->model('Accountrecharge_model');
		$vdata = $this->Accountrecharge_model->queryFloatCount();
		
		$this->load->view('/sys_admin/statistics/floatcount', $vdata);//TODO  暂略
		
		//TODO  暂略
		/**
		$data = array();
		$per_page 	= $this->input->get('per_page')?$this->input->get('per_page'):0;
		$strtime	= $this->input->get('strtime');
		$endtime	= $this->input->get('endtime');
		
		if($strtime != ''){
			$data['where']['strtime'] = $strtime;
		}
		if($endtime != ''){
			$data['where']['endtime'] = $endtime;
		}
		$data['page_size']  = STATISTICS_LIST_PAGE_SIZE;//每页行数
		$data['page_start'] = (int)$per_page;//起始页
		
		
		$this->load->model('User_model');
		$vdata = $this->User_model->statistics_user($data);
		
		$this->load->view('/sys_admin/statistics/floatcount', $vdata);
		*/
		
		//$vdata['tab'] = $this->tab;
		//$vdata['sub'] = 'platformcount';
	}
	
	
	/*
	 * 风险金准备金统计分析表
	 */
	function riskcount(){
	
			
		//TODO  暂略
	$vdata['tab'] = $this->tab;
		$vdata['sub'] = 'riskcount';
		
		$this->load->view('/sys_admin/statistics/riskcount', $vdata);
	}
	
	
	/*
	 * 会员数据统计分析表
	 */
	function udatacount(){
		
		$data = array();
		$per_page 	= $this->input->get('per_page')?$this->input->get('per_page'):0;
		$strtime	= $this->input->get('strtime');
		$endtime	= $this->input->get('endtime');
		
		if($strtime != ''){
			$data['where']['strtime'] = $strtime;
		}
		if($endtime != ''){
			$data['where']['endtime'] = $endtime;
		}
		$data['page_size']  = STATISTICS_LIST_PAGE_SIZE;//每页行数
		$data['page_start'] = (int)$per_page;//起始页
		
		$this->load->model('User_model');
		$vdata = $this->User_model->statistics_user($data);

		//分页
		$this->load->library('pagination');
		$config['base_url'] = '/sys_admin/statistics/udatacount/?strtime='.$strtime.'&endtime='.$endtime;
		$config['total_rows'] = $vdata['time_count'];;
		$config['per_page'] = STATISTICS_LIST_PAGE_SIZE; 
		$this->pagination->initialize($config); 
		$vdata['tab'] = $this->tab;
		$vdata['sub'] = 'usercount';
		
		$this->load->view('/sys_admin/statistics/udatacount', $vdata);
	}
	
	
	/*
	 * 理财情况统计分析表
	 */
	function managemoney($type = "default"){
		$year 	= $_REQUEST['year'];
		$month 	= $_REQUEST['month'];
		$ji		= $_REQUEST['ji'];
		
		$tstart = trim($_REQUEST['tstart']);
		$tend 	= trim($_REQUEST['tend']);
		
		$vdata['tab'] = $this->tab;
		$vdata['sub'] = 'managecount';

		$currt = strtotime("2015-01-01");
		$start = strtotime("2015-01-01");
		
		//$currt = strtotime("2014-02-01");
		//$start = strtotime("2014-02-01");
		
		if($tstart != ""){
			$currt = strtotime($tstart.'-01');
			$start = strtotime($tstart.'-01');
		}
		$end   = strtotime(date("Y-m-01"));
		$end 	= strtotime("1 month",$end);
		if($tend != ""){
			$end = strtotime($tend.'-01');
			$end = strtotime("1 month",$end);
		}
		$this->load->model('Borrow_model');
		$nstart = 0;
		$index = 0;
		do{
			$nstart = strtotime("1 month",$start);
			$vdata['list'][$index] = $this->Borrow_model->managemoney(array('start_time' =>$start, 'end_time'=> $nstart));
			$vdata['list'][$index]['year'] = date("Y",$start);
			$vdata['list'][$index]['month'] = date("m",$start);
			$start = strtotime("1 month",$start);
			$index++;
		}while($start != $end);
		
		$vdata['tstart'] 	= $tstart;
		$vdata['tend'] 	= $tend;
		
		if($type == 'caccount'){
			self::array_sort($vdata['list'],'caccount','DESC');
		}elseif($type == 'saccount'){
			self::array_sort($vdata['list'],'saccount','DESC');
		}elseif($type == 'useracount'){
			self::array_sort($vdata['list'],'useracount','DESC');
		}elseif($type == 'museracount'){
			self::array_sort($vdata['list'],'museracount','DESC');
		}elseif($type == 'rjtmoney'){
			self::array_sort($vdata['list'],'rjtmoney','DESC');
		}elseif($type == 'baccount'){
			self::array_sort($vdata['list'],'baccount','DESC');
		}elseif($type == 'rjcnum'){
			self::array_sort($vdata['list'],'rjcnum','DESC');
		}elseif($type == 'rjye'){
			self::array_sort($vdata['list'],'rjye','DESC');
		}
		//TODO  暂略
		$this->load->view('/sys_admin/statistics/managemoney', $vdata);
	}
	
	
	function managemoneyexcel($type = "default"){
		$year 	= $_REQUEST['year'];
		$month 	= $_REQUEST['month'];
		$ji		= $_REQUEST['ji'];
		
		$tstart = trim($_REQUEST['tstart']);
		$tend 	= trim($_REQUEST['tend']);
		
		$vdata['tab'] = $this->tab;
		$vdata['sub'] = 'managecount';
		$currt = strtotime("2014-02-01");
		$start = strtotime("2014-02-01");
		if($tstart != ""){
			$currt = strtotime($tstart.'-01');
			$start = strtotime($tstart.'-01');
		}
		$end   = strtotime(date("Y-m-01"));
		$end 	= strtotime("1 month",$end);
		if($tend != ""){
			$end = strtotime($tend.'-01');
			$end = strtotime("1 month",$end);
		}
		$this->load->model('Borrow_model');
		$nstart = 0;
		$index = 0;
		do{
			$nstart = strtotime("1 month",$start);
			$vdata['list'][$index] = $this->Borrow_model->managemoney(array('start_time' =>$start, 'end_time'=> $nstart));
			$vdata['list'][$index]['year'] = date("Y",$start);
			$vdata['list'][$index]['month'] = date("m",$start);
			$start = strtotime("1 month",$start);
			$index++;
		}while($start != $end);
		
		if($type == 'caccount'){
			self::array_sort($vdata['list'],'caccount','DESC');
		}elseif($type == 'saccount'){
			self::array_sort($vdata['list'],'saccount','DESC');
		}elseif($type == 'useracount'){
			self::array_sort($vdata['list'],'useracount','DESC');
		}elseif($type == 'museracount'){
			self::array_sort($vdata['list'],'museracount','DESC');
		}elseif($type == 'rjtmoney'){
			self::array_sort($vdata['list'],'rjtmoney','DESC');
		}elseif($type == 'baccount'){
			self::array_sort($vdata['list'],'baccount','DESC');
		}elseif($type == 'rjcnum'){
			self::array_sort($vdata['list'],'rjcnum','DESC');
		}elseif($type == 'rjye'){
			self::array_sort($vdata['list'],'rjye','DESC');
		}
		
		$this->load->library('PHPExcel');
		$this->load->library('PHPExcel/IOFactory'); 
		$objPHPExcel = new PHPExcel();
		$objPHPExcel->getProperties()->setTitle("理财情况统计分析表")->setDescription(COMPANY_NAME); 
		
		$objPHPExcel->setActiveSheetIndex(0);
		$titles = array("序号", "年", "月", "累计理财投标总额", "本月新增理财总额", "累计理财会员数", "新增理财会员数", "人均理财金额", "每标人均投标金额", "人均投资债权标数量", "人均账户余额");
		
		for($i = 0; $i < count($titles); $i ++){
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($i, 1, $titles[$i]);
		}
		$account_list = $vdata['list'];
		for($i = 0; $i < count($account_list); $i++){
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(0, $i+2, $i+1);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(1, $i+2, $account_list[$i]['year']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(2, $i+2, $account_list[$i]['month']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(3, $i+2, $account_list[$i]['caccount']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(4, $i+2, $account_list[$i]['saccount']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(5, $i+2, $account_list[$i]['useracount']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(6, $i+2, $account_list[$i]['museracount']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(7, $i+2, $account_list[$i]['rjtmoney']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(8, $i+2, $account_list[$i]['baccount']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(9, $i+2, $account_list[$i]['rjcnum']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(10, $i+2, round($account_list[$i]['rjye'],2));
		}
		$objWriter = IOFactory::createWriter($objPHPExcel, 'Excel5'); 
		//发送标题强制用户下载文件 
		header('Content-Type: application/vnd.ms-excel'); 
		header('Content-Disposition: attachment;filename="理财情况统计分析表.xls"'); 
		header('Cache-Control: max-age=0'); 
		$objWriter->save('php://output'); 
	}
	
	function array_sort($array,$keys,$type='asc'){
	    if(!isset($array) || !is_array($array) || empty($array)){
	        return '';
	    }
	    if(!isset($keys) || trim($keys)==''){
	        return '';
	    }
	    if(!isset($type) || $type=='' || !in_array(strtolower($type),array('asc','desc'))){
	        return '';
	    }
	    $keysvalue=array();
	    foreach($array as $key=>$val){
	        $val[$keys] = str_replace('-','',$val[$keys]);
	        $val[$keys] = str_replace(' ','',$val[$keys]);
	        $val[$keys] = str_replace(':','',$val[$keys]);
	        $keysvalue[] =$val[$keys];
	    }
	    asort($keysvalue); //key值排序
	    reset($keysvalue); //指针重新指向数组第一个
	    foreach($keysvalue as $key=>$vals) {
	        $keysort[] = $key;
	    }
	    $keysvalue = array();
	    $count=count($keysort);
	    if(strtolower($type) != 'asc'){
	        for($i=$count-1; $i>=0; $i--) {
	            $keysvalue[] = $array[$keysort[$i]];
	        }
	    }else{
	        for($i=0; $i<$count; $i++){
	            $keysvalue[] = $array[$keysort[$i]];
	        }
	    }
	    return $keysvalue;
	}
	
	/*
	 * 流量统计分析
	 */
	function flowcount(){
		$vdata['tab'] = $this->tab;
		$vdata['sub'] = 'flowcount';
		
			
		//TODO  暂略
		
		$this->load->view('/sys_admin/statistics/flowcount', $vdata);
	}
	
	
	
>>>>>>> d47623b610d79a4eeb489500830c00d70933f414
}